﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    public class FinPlusGridColBase : DataGridTemplateColumn, IFinPlusComp
    {
        public virtual bool IsValid { get; set; }
        public string Id { get; set; }
        public string LinkedId { get; set; }
        public string Config { get; set; }
        public string TestString { get; private set; }
        public Env Env { get; set; }
        public IPods Adapter { get { return null; } }
        public static IPods Structure { get; private set; }

        private volatile static ResourceDictionary _images, _other;
        private volatile static Object _lock = new Object();

        //construct
        public FinPlusGridColBase()
        {
            if (_images == null)
            {
                lock (_lock)
                { 
                    if (_images == null)
                    {
                        _images = new ResourceDictionary { Source = new Uri("pack://application:,,,/FinPlusCompView;component/Resource.Images.xaml", UriKind.RelativeOrAbsolute) };
                        _other = new ResourceDictionary { Source = new Uri("pack://application:,,,/FinPlusCompView;component/Resource.Other.xaml", UriKind.RelativeOrAbsolute) };
                        Structure = new FinPlusControl().Structure;
                    }
                }
            }
       }

        //common control interface 
        public void Dock(object component, string dock) 
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public void CmdExecute(IPod command)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public void Dispose() 
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        //protected
        protected StackPanel HeaderFilterCheck(string name, CheckBox filterCheck, string image = null)
        {
            var stackPanel = new StackPanel() { Tag = name, Orientation = Orientation.Horizontal };

            var header = filterCheck;
            if (image != null)
                stackPanel.Children.Add(new Image() { Source = (BitmapImage)_images[image], Width = 9, Height = 9, VerticalAlignment = VerticalAlignment.Center });
            stackPanel.Children.Add(new TextBlock() { Text = " " });
            stackPanel.Children.Add(header);

            return stackPanel;
        }

        protected StackPanel HeaderTextBlock(string name, string image = null)
        {
            var stackPanel = new StackPanel() { Tag = name, Orientation = Orientation.Horizontal };
            if (image != null) stackPanel.Children.Add(new Image() { Source = (BitmapImage)_images[image], Width = 9, Height = 9, VerticalAlignment = VerticalAlignment.Center });
            stackPanel.Children.Add(new TextBlock() { Text = name + " " });
            return stackPanel;
        }

        protected DataTemplate TextBlockCell(string name, string stringFormat = null, string type = null)
        {
            var factory = new FrameworkElementFactory(typeof(TextBlock));

            var b = new Binding(name);
            if (stringFormat != null)
                b.StringFormat = stringFormat;

            if (type != null && type.Equals("Num"))
                factory.SetValue(Image.StyleProperty, (Style)_other["numTextBlock"]);

            factory.SetBinding(TextBlock.TextProperty, b);

            return new DataTemplate() { VisualTree = factory };
        }

        protected DataTemplate ToggleImageCell(string name, string imageStyle)
        {
            var factory = new FrameworkElementFactory(typeof(Image));

            var b = new Binding(name);

            factory.SetBinding(Image.TagProperty, b);
            factory.SetValue(Image.StyleProperty, (Style)_other[imageStyle]);

            return new DataTemplate() { VisualTree = factory };
        }

        protected DataTemplate ButtonImageCell(string imageStyle)
        {
            var factory = new FrameworkElementFactory(typeof(Image));

            factory.SetValue(Image.StyleProperty, (Style)_other[imageStyle]);

            return new DataTemplate() { VisualTree = factory };
        }
    }
}
